//
//  Popwindow.m
//  微他
//
//  Created by apple on 14-11-21.
//  Copyright (c) 2014年 apple. All rights reserved.
//

#import "Popwindow.h"
@interface Popwindow(){
    UIView *view;
    UIView *spaceView;
    CGFloat height;
    BOOL ishow;
}

@property (nonatomic) NSArray *dataarray;
@property (nonatomic) NSInteger uid;

@end
@implementation Popwindow

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame data:(NSArray *)array uid:(NSInteger) uid{
    self=[super initWithFrame:frame];
    if (self) {
        _dataarray=array;
        _uid=uid;
        ishow=NO;
        height = 44 * _dataarray.count + 25 * 3 + (_dataarray.count - 2) * 8;
        [self initView];
    }
    return self;
}

- (void)initView{
    self.backgroundColor = [UIColor clearColor];
    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
    view.backgroundColor = [UIColor grayColor];
    view.alpha = 0;
    view.tag = 699;
    [self addSubview:view];
    spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(view.bounds), height)];
    spaceView.backgroundColor = [UIColor clearColor];
    spaceView.tag = 700;
    [self addSubview:spaceView];
    for (int i=0; i<_dataarray.count; i++) {
        UIButton *button;
        if (i!=0 && i!=_dataarray.count-1) {
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            if (i==1) {
                [button setImage:[UIImage imageNamed:@"wechat"] forState:UIControlStateNormal];
            }else if (i==2) {
                [button setImage:[UIImage imageNamed:@"messagesmall"] forState:UIControlStateNormal];
            }else if (i==3) {
                [button setImage:[UIImage imageNamed:@"weibosmall"] forState:UIControlStateNormal];
            }
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
        }else
            button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.backgroundColor = [UIColor whiteColor];
        button.layer.cornerRadius = 2;
        button.frame = CGRectMake(0, 0, 278, 44);
        button.center = CGPointMake(CGRectGetMidX(self.bounds), 77 + 44 * i);
        
        if (i==0) {
            button.enabled = NO;
        }
        if (i==_dataarray.count - 1) {
            button.center = CGPointMake(CGRectGetMidX(self.bounds), 77 + 44 * i + 17);
        }
        [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [button setTintColor:[UIColor colorWithRed:55/255.0 green:55/255.0 blue:55/255.0 alpha:1]];
        if (i == _dataarray.count - 1) {
//            button.backgroundColor = [UIColor colorWithRed:154 / 255.0 green:154 / 255.0 blue:154 / 255.0 alpha:1];
            [button setTintColor:[UIColor redColor]];
        }else
        {
            button.layer.borderWidth = 0.2;
            button.layer.borderColor = [UIColor lightGrayColor].CGColor;
        }
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:_dataarray[i] forState:UIControlStateNormal];
        [spaceView addSubview:button];
    }
    ishow=YES;
    [UIView animateWithDuration:0.2 animations:^{
        view.alpha = 0.3;
        spaceView.frame = CGRectMake(0, CGRectGetHeight(self.bounds) -  height, CGRectGetWidth(view.bounds), height);
    }];
}

- (void)click:(UIButton *)sender{
    if ([sender.titleLabel.text isEqualToString:@"取消"]) {
        [self cancle];
    }else{
        [_delegate PopupDelegate:sender.titleLabel.text uid:_uid];
        [self cancle];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if (touch.view.tag != 700) {
        [self cancle];
    }
}

- (BOOL)isshow{
    return ishow;
}

- (void)cancle{
    UIView *curview = (UIView *)[self viewWithTag:700];
    UIView *tempview = (UIView *)[self viewWithTag:699];
    [UIView animateWithDuration:0.2 animations:^{
        curview.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(curview.bounds), height);
        tempview.alpha = 0;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    ishow=NO;
    return;
}


@end
